#!/bin/bash

#------------------------------------------------------------------------------
#Filename:      nfs-config.sh
#Version:       1.0
#Date:  	2013/09/04
#Author:        yuanhui.shi
#Email: 	yuanhui.shi@cs2c.com.cn
#Summary:       配置nfs服务器
#Notes: 	验证nfs服务器能否搭建成功
#Copyright:     China Standard Software Co.,Ltd.
#History:
#      		Version 1.0,2013/07/11
#       	-The first one
#------------------------------------------------------------------------------
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH


#################################
#
# start, stop, restart service
#
#################################
if command -v systemctl >/dev/null 2>&1; then
    HAVE_SYSTEMCTL=1
else
    HAVE_SYSTEMCTL=0
fi

##! @TODO: start service
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
function start_daemon()
{
        if [ $HAVE_SYSTEMCTL -eq 1 ]; then
                systemctl start $1.service > /dev/null 2>&1
        else
                service $1 start > /dev/null 2>&1
        fi
}

function stop_daemon()
{
        if [ $HAVE_SYSTEMCTL -eq 1 ]; then
                systemctl stop $1.service > /dev/null 2>&1
        else
                service $1 stop > /dev/null 2>&1
        fi
}

function status_daemon()
{
        if [ $HAVE_SYSTEMCTL -eq 1 ]; then
                systemctl status $1.service > /dev/null 2>&1
        else
                service $1 status > /dev/null 2>&1
        fi
}

function restart_daemon()
{
        if [ $HAVE_SYSTEMCTL -eq 1 ]; then
                systemctl restart $1.service > /dev/null 2>&1
        else
                service $1 restart > /dev/null 2>&1
        fi
}

#set enviroment

stop_daemon iptables 
stop_daemon nfs

#create two directories
[ -d /nfs-test/readonly ] || mkdir -p /nfs-test/readonly
[ -f /nfs-test/readonly/file1 ] || touch /nfs-test/readonly/file1
[ -d /nfs-test/rw ] || mkdir -p /nfs-test/rw
chmod -R 777 /nfs-test/readonly /nfs-test/rw /nfs-test

#Modify the main conf file of nfs service
echo "/nfs-test/readonly *(ro)" >>/etc/exports
echo "/nfs-test/rw *(rw)" >>/etc/exports

#Start nfs service
restart_daemon nfs

sleep 60

